// patrolnpc.txt
// Creature attacks anything it hates nearby, and spends the rest of its time patrolling a path.
// Once it has won, it returns to its home.
// Memory Cells:
//   Cell 0 - Number of path to follow.
//	 Cell 1 - If 0, just loops around path. If 1, walks to end of path, waits a little, and returns home.
//      If 2, goes to end of path and just stops.
//   Cell 2 - what sort of ae

begincreaturescript;

variables;

short i,target;
short ae_toggle;
short last_abil;
short ae_range = 7;

body;

beginstate INIT_STATE;
	place_particle_num(ME,18,12,4);		
	place_particle_num(ME,19,12,4);		
	place_particle_num(ME,20,12,4);		

	set_aggression(ME,10);
	set_attack_bonus(ME,6);
	
	last_abil = get_current_tick();
	break;

beginstate DEAD_STATE;
	inc_flag(25,2,1);
break;

beginstate START_STATE;
	if (get_foe_target(ME,6,0)) {
		do_attack();
		set_state(3);
		}
	if (who_shot_me() >= 0) {
		set_foe_target(ME,who_shot_me());
		do_attack();
		set_state(3);
		}
	if (get_memory_cell(0) < 12)  {
			if (my_dist_from_start() >= 2)  {
				if (get_ran(1,1,100) < 50) 
					return_to_start(ME,1);
				}
			if (get_ran(1,1,100) < 40) {
				follow_path(ME,get_memory_cell(0),0);
				set_state(4);
				}
			}

	if (am_i_doing_action() == FALSE)
		end_combat_turn();
break;

beginstate 3; // attacking
	if (target_ok() == FALSE)
		set_state(START_STATE);

	if ((is_combat()) && 
	  (tick_difference(last_abil,get_current_tick()) > 0)) {
		run_char_animation(2,1,35);	

	  	if ((get_memory_cell(2) == 0) && (ae_toggle == 0)) { // debuff
		  	ae_toggle = 1;
		  	place_particle_num(ME,3,6,8);
			spray_missiles(43,8,ae_range);
			print_named_str(ME,"radiates an aura of static.");
			pc_heard_sound_delay(201,250);						
			particle_nearby(ae_range,8,2,4,2);
			status_nearby(-10,ae_range,0,0);
			status_nearby(-10,ae_range,1,0);
			status_nearby(-10,ae_range,8,0);
			status_nearby(-10,ae_range,9,0);
			status_nearby(-10,ae_range,11,0);
			status_nearby(-10,ae_range,12,0);
			status_nearby(-10,ae_range,13,0);
			status_nearby(-10,ae_range,14,0);
			status_nearby(-10,ae_range,15,0);
			status_nearby(-10,ae_range,16,0);
			status_nearby(-10,ae_range,18,0);
			status_nearby(-10,ae_range,19,0);
	  		}
			else if ((get_memory_cell(2) == 0) && (ae_toggle == 1)) { // fear
				ae_toggle = 0;
				place_particle_num(ME,4,6,8);
				print_named_str(ME,"emits an aura of terror.");
				pc_heard_sound_delay(107,250);						
				status_nearby(get_level(ME) + 5 + get_attack_bonus(ME),ae_range,7,0);
				}
	  	if ((get_memory_cell(2) == 1) && (ae_toggle == 0)) { // acid
		  	ae_toggle = 1;
		  	place_particle_num(ME,4,6,8);
			print_named_str(ME,"emits an acid spray.");
			pc_heard_sound_delay(177,250);			
	  		create_missile_spiral(142,40,ae_range,2);
			status_nearby(get_level(ME) + 5 + get_attack_bonus(ME),ae_range,6,0);
	  		}
			else if ((get_memory_cell(2) == 1) && (ae_toggle == 1)) { // fire
				ae_toggle = 0;
				place_particle_num(ME,2,4,8);
				print_named_str(ME,"surrounds itself with a curtain of flame.");
				pc_heard_sound_delay(117,250);						
				create_missile_spiral(154,40,ae_range,2);
				damage_nearby(get_ran(get_level(ME) + get_attack_bonus(ME),1,6),ae_range,2,0);
				}

	  	if ((get_memory_cell(2) == 2) && (ae_toggle == 0)) { // ice
		  	ae_toggle = 1;
		  	place_particle_num(ME,1,4,8);
			print_named_str(ME,"radiates a cloud of icy particles.");
			pc_heard_sound_delay(165,250);						
	  		create_missile_spiral(151,40,ae_range,2);
	  		damage_nearby(get_ran(get_level(ME) + get_attack_bonus(ME),1,8),ae_range,6,0);
	  		}
	  		else if ((get_memory_cell(2) == 2) && (ae_toggle == 1)) { 
				ae_toggle = 0;
				place_particle_num(ME,1,4,8);
				print_named_str(ME,"surrounds itself with an aura of lightning.");
				pc_heard_sound_delay(179,250);						
				create_missile_spiral(147,40,ae_range,2);
				damage_nearby(get_ran(get_level(ME) + get_attack_bonus(ME),1,5),ae_range,1,0);
				status_nearby(get_level(ME) / 4,ae_range,20,0);
				}

		last_abil = get_current_tick();
		end();
		}
	do_attack();
break;

beginstate 4; // patrol
	if (get_foe_target(ME,8,0)) {
		do_attack();
		set_state(3);
		}
	if (who_shot_me() >= 0) {
		set_foe_target(ME,who_shot_me());
		do_attack();
		set_state(3);
		}
	if (follow_path(ME,get_memory_cell(0),0) == TRUE)
		set_state(START_STATE);
break;

beginstate TALKING_STATE;
	if (get_memory_cell(2) == 0) {
		print_str("Talking: It doesn't respond.");
		}
		else begin_talk_mode(get_memory_cell(2));	
break;